package com.wwdx.controller.admin;

import com.google.common.collect.Maps;
import com.wwdx.model.upms.UpmsUser;
import com.wwdx.service.upms.UpmsApiServiceI;
import com.wwdx.service.upms.UpmsUserServiceI;
import com.wwdx.shiro.GlobalStatic;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

/**
 * @author liguoliang
 * @date 2018/3/2 16:07
 */
@Controller
@RequestMapping("/system/upms_user")
public class UpmsUserController {
    
    @Resource
    private UpmsUserServiceI upmsUserService;
    @Resource
    private UpmsApiServiceI upmsApiService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public ModelAndView list(UpmsUser model,
                             @RequestParam(value = "page", defaultValue = "1") int page,
                             @RequestParam(value = "rows",defaultValue = "10") int rows) {

        ModelAndView mav = new ModelAndView();
        mav.addObject("listSize",  upmsUserService.selectModelCount(model));
        mav.addObject("list",  upmsUserService.selectModelList(model,page,rows));
        return mav;
    }

    /**
     * 新增
     */
    @RequestMapping("/create")
    public ModelAndView create() {
        return new ModelAndView();
    }

    /**
     * 修改页面
     */
    @RequestMapping("/edit")
    public ModelAndView edit(Integer userId) {
        ModelAndView mav = new ModelAndView();
        mav.addObject("model",upmsUserService.getById(userId));
        return mav;
    }

    /**
     * 保存新增
     */
    @RequestMapping("/save")
    public String save(UpmsUser model){
        model.setGmt_created(new Date());
        model.setGmt_modified(new Date());
        String salt = UUID.randomUUID().toString().replaceAll("-", "");
        int hashIterations = GlobalStatic.HASHITERATIONS;
        String md5Pwd = new Md5Hash(model.getPassword(), model.getAccount() + salt, hashIterations).toHex();
        model.setSalt(salt);
        model.setPassword(md5Pwd);
        model.setLocked(0);
        upmsUserService.save(model);
        return "redirect:/system/upms_user/list";
    }

    /**
     * 修改保存
     */
    @RequestMapping("/update")
    public String update(UpmsUser model){
        Integer userId = model.getUserId();
        UpmsUser upmsUser = upmsUserService.getById(userId);
        upmsUser.setRealname(model.getRealname());
        upmsUser.setSex(model.getSex());
        upmsUser.setPhone(model.getPhone());
        upmsUser.setEmail(model.getEmail());
        upmsUser.setGmt_modified(new Date());
        upmsUserService.update(upmsUser);
        return "redirect:/system/upms_user/list";
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresRoles("super")
    public String delete(Integer userId) {
        upmsUserService.delete(upmsUserService.getById(userId));
        return "redirect:/system/upms_user/list";
    }

    /**
     * 验证账户是否存在
     */
    @RequestMapping("/validate")
    @ResponseBody
    public Map<String,Object> validate(String account) {
        UpmsUser upmsUser = upmsApiService.selectUpmsUserByUsername(account);
        boolean result = false;
        if (upmsUser == null){
            result = true;
        }
        Map<String, Object> params = Maps.newHashMapWithExpectedSize(2);
        params.put("valid", result);
        return params;
    }
}